package com.ibeeking.found.cms.cp.rest.service.impl;

import com.ibeeking.found.cms.cp.rest.service.IPageBaseService;
import com.ibeeking.found.cms.service.common.bo.PageBaseBO;
import com.ibeeking.found.cms.service.common.constants.RedisKey;
import com.ibeeking.found.cms.service.common.constants.enums.PageCategoryEnum;
import com.ibeeking.found.cms.service.common.dos.PageBaseDO;
import com.ibeeking.found.cms.service.common.query.PageBaseQuery;
import com.ibeeking.found.cms.service.mapper.PageBaseMapper;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.redis.utils.RedisUtils;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName PageBaseServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-05-08 16:05
 **/
@Service
public class PageBaseServiceImpl extends ServiceImpl<PageBaseMapper, PageBaseDO> implements IPageBaseService {
    @Resource
    private RedisUtils redisUtils;

    @Resource
    private PageBaseMapper pageBaseMapper;

    @Override
    public List<PageBaseBO> list(PageBaseQuery query) {
        List<PageBaseDO> pageBaseDOs = pageBaseMapper.selectList(new LambdaQueryWrapper<PageBaseDO>()
                .eq(query.getCategory() != null, PageBaseDO::getCategory, query.getCategory())
                .in(Collections3.isNotEmpty(query.getIds()), PageBaseDO::getId, query.getIds())
        );
        return JsonUtils.jsonToList(pageBaseDOs, PageBaseBO.class);
    }


    //@CacheQuery(cacheName = RedisKey.PAGE_BASE, key = "#id")
    @Override
    public PageBaseBO selById(Long id) {
        PageBaseDO pageBaseDO = pageBaseMapper.selectById(id);
        return BeanUtil.convertBean(pageBaseDO, PageBaseBO.class);
    }

    //@CacheQuery(cacheName = RedisKey.PAGE_BASE, key = "#tenantId+'-'+#pageCategoryEnum.value")
    @Override
    public PageBaseBO selOneByPageCategory(PageCategoryEnum pageCategoryEnum, String tenantId) {
        if (pageCategoryEnum == null) {
            return null;
        }
        PageBaseQuery pageQuery = new PageBaseQuery();
        pageQuery.setCategory(pageCategoryEnum.getValue());
        List<PageBaseBO> list = getBean().list(pageQuery);
        if (Collections3.isEmpty(list)) {
            return null;
        }
        if (pageCategoryEnum.getHasOnlyOne() && Collections3.size(list) > 1) {
            DataUtils.trueThrowException(true, "无法获取页面(页面重复)");
        }
        return list.get(0);
    }

    @Override
    public Boolean removeCacheByIds(Long... ids) {
        List<Long> idList = Arrays.asList(ids);
        if (Collections3.isEmpty(Arrays.asList(ids))) {
            return true;
        }
        List<String> redisKeys = new ArrayList<>();
        idList.forEach(id -> {
            redisKeys.add(RedisKey.PAGE_BASE + id);
            redisKeys.add(RedisKey.PAGE_MODULE + id);
            redisKeys.add(RedisKey.PAGE_MODULE_LIST + id);
        });
        //根据页面id获取页面类型 再根据页面类型 PAGE_BASE 下的缓存
        List<PageBaseBO> pageBaseBOS = getBean().list(new PageBaseQuery().setIds(idList));
        if (Collections3.isNotEmpty(pageBaseBOS)) {
            pageBaseBOS.forEach(x -> {
                redisKeys.add(RedisKey.PAGE_BASE + UserUtils.getTenantId() + "-" + x.getCategory());
            });
        }
        redisUtils.del(redisKeys);
        return true;
    }

    private PageBaseServiceImpl getBean() {
        return SpringBeanUtil.getBean(this.getClass());
    }

}